$(function () {
    $("#page").load(URI + "/rbac/html/page.html");
});

//查询所有数据
function findAll() {
    let data = {
        username: $("#username").val(),
        email: $("#email").val(),
        phone: $("#phone").val(),
        pageNum: $("#pageNum").val(),
        pageSize: $("#pageSize").val()
    };
    let result = myAjax("/api/rb/user/findAll", data, "post");
    // console.log(resultList);
    // let  result = adminLogin(resultList);
    // 总条数
    if ($.isEmptyObject(result)) {
        alert("您没有权限访问!!!")

    }
    if (result) {
        if (result.data.count != null) {
            $("#totalCount").html(result.data.count);
        }
        setData(result.data.pageInfo.list);
        setBtn(result.data.pageInfo.list);
    }
}

function setData(data) {
    let html = "";
    for (let i = 0; i < data.length; i++) {
        html += " <tr >\n" +
            "            <td>" + data[i].id + "</td>\n" +
            "            <td title='" + data[i].username + "'>" + clearNull(data[i].username) + "</td>\n" +
            "            <td title='" + data[i].icon + "'><img src='" + clearNull(URI + IMG + data[i].icon) + "' style='width: 30px; height: 30px;' ></td>\n" +
            "            <td title='" + data[i].phone + "'>" + clearNull(data[i].phone) + "</td>\n" +
            "            <td title='" + data[i].email + "'>" + clearNull(data[i].email) + "</td>\n" +
            "            <td title='" + data[i].roleName + "'>" + clearNull(data[i].roleName ) + "</td>\n" +
            "            <td class=\"td-status\">\n" +
            "              <span class=\"layui-btn layui-btn-normal layui-btn-mini\">" + (clearNull(data[i].status) === 1 ? "正常" : "<span style='color: red;'>冻结<span/>") + "</span></td>\n" +
            "            <td class=\"td-manage\">\n" +
            "                   <button class=\"layui-btn layui-btn layui-btn-xs\"  onclick=\"assignRole(" + data[i].id + ")\" >" +
            "                   <i class=\"layui-icon\">&#xe642;</i>分配角色</button>" +
            "                   <button id='delClock" + data[i].id + "' style='' class=\"layui-btn layui-btn layui-btn-xs\"  onclick=\"delClockUser(" + data[i].id + ")\" >" +
            "                   <i class=\"layui-icon\">&#xe642;</i>解冻账户</button>" +
            "                   <button id='clock" + data[i].id + "' style='' class=\"layui-btn-danger layui-btn layui-btn-xs\"  onclick=\"clockUser(" + data[i].id + ")\" >" +
            "                   <i class=\"layui-icon\">&#xe673;</i>冻结账户</button>" +
            "                   <button class=\"layui-btn-danger layui-btn layui-btn-xs\"  onclick=\"delUserRole(" + data[i].id + "," + data[i].roleId + ")\" >" +
            "                   <i class=\"layui-icon\">&#xe640;</i>删除用户角色</button>" +
            "            </td>\n" +
            "          </tr>";

    }
    $("#tbody").html(html);
}

/***
 * 清除数据中的undefined和null将其置换为空，不显示
 */

function clearNull(value) {
    return value === "null" || value === undefined ? "" : value;
}

/***
 * 删除功能
 */
function delUserRole(id, roleId) {
    let data = {
        userId: id,
        roleId: roleId
    };
    let result = myAjax("/api/rb/urr/delUserRole", data, "delete");
    if (result.code === 200) {
        //删除成功，就重新查询数据
        alert("删除成功(== ^^ ==)!");
        findAll();
    } else {
        alert("删除失败(╥╯^╰╥)！")
    }
}


/**
 * 分配角色
 */
function assignRole(id) {
    sessionStorage.setItem("userId", id);
    x_admin_show('分配角色', URI + '/rbac/html/user/user-assignRole.html', 600, 600);
}

/**
 * 冻结账户
 */
function clockUser(id) {
    sessionStorage.setItem("clockId", id);
    x_admin_show('冻结账户', URI + '/rbac/html/user/user-clockUser.html', 300, 300);
}

/**
 * 解冻账户
 */
function delClockUser(id) {
    let result = myAjax("/api/se/user/delClockUser", {id: id}, "post");
    if (result.code === 200) {
        layer.alert(result.data, {icon: 1});
        // alert(result.data);
    } else {
        layer.alert(result.data, {icon: 2});
    }

}

/**
 * 隐藏按钮
 */
function setBtn(data) {
    for (let i = 0; i < data.length; i++) {
        if (data[i].status === 0) {//若账号为冻结状态则隐藏冻结按钮，显示解冻按钮
            $("#clock" + data[i].id).attr("style", "display:none;");
        } else {//若账号为正常状态则隐藏解冻按钮，显示冻结按钮
            $("#delClock" + data[i].id).attr("style", "display:none;");
        }
    }
}
